<%-- 
    Document   : administracaoPerfilEditar
    Created on : 21/10/2010, 11:45:00
    Author     : Thiago
--%>

<%@page import="dao.*"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="crypt.Crypt"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="bo.*"%>
<%@page import="beans.*"%>
<%@page import="autenticacao.Sessao"%>
<%
            String moduleId = "23";
            Crypt crypt = new Crypt();
            try {
                crypt.setKey(crypt.Decifragem((String) session.getAttribute("userPhrase")));
                String desafio = crypt.Cifragem(moduleId + "@" + ferramentas.DataHora.formatarDataHora(ferramentas.DataHora.retornarDataHoraAtual(), ferramentas.DataHora.DATE_TIME_FORMAT));
                if (!(new Sessao(session).validarSessao(desafio))) {
                    response.sendRedirect("/" + request.getContextPath());
                    response.flushBuffer();
                    out.println("<script language=\"JavaScript\"> window.location.replace('" + (new String(request.getRequestURL()).split("pages")[0]) + "); </script>");
                    out.flush();
                }
            } catch (Exception e) {
                response.sendRedirect("/" + request.getContextPath());
            }

            String perfil = (String) request.getParameter("perfil");

            Perfil pfl = null;
            try {
                pfl = new PerfilDAO().recuperarPerfil(Integer.parseInt(perfil));
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }

            if ((pfl == null)
                    || (perfil == null)
                    || perfil.isEmpty()) {
                out.println("<script language=\"JavaScript\"> window.location.replace('" + (new String(request.getRequestURL()).split("pages")[0]) + "pages/administracaoPerfilListar.jsp'); </script>");
                out.flush();
            }
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Gestor Operacional</title>
        <link href="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>css/style.css" type="text/css" rel="stylesheet"/>
        <link href="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>css/table.css" type="text/css" rel="stylesheet"/>
        <link href="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>css/anytime.css" rel="stylesheet" type="text/css"/>
        <script type="text/javascript" src="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>scripts/jquery.js"></script>
        <script type="text/javascript" src="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>scripts/anytime.js"></script>
        <script type="text/javascript" src="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>scripts/anytimetz.js"></script>
        <script type="text/javascript" src="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>scripts/table.js"></script>
    </head>
    <body>
        <table width="100%">
            <tr>
                <td>
                    <jsp:include page = "../menu.jsp" flush = "true"/>
                </td>
            </tr>

            <tr align="center">
                <td><br/><br/>
                    <form name="form1" name="form1" method="post" action="administracaoPerfilEditar.jsp">
                        <input type="hidden" name="action" value="executar"/>
                        <%

                                    String action = (String) request.getParameter("action");
                                    if ((action != null) && (action.equalsIgnoreCase("executar"))) {
                                        try {
                                            ArrayList<Modulo> amod = new ModuloDAO().recuperarModulos(null);
                                            if ((amod != null) && (!amod.isEmpty())) {
                                                ModuloPerfil mp = new ModuloPerfil();
                                                mp.setPerfil(pfl);
                                                new ModuloPerfilDAO().excluirModPerfil(mp);
                                                for (int i = 0; i < amod.size(); i++) {
                                                    if ((Integer.valueOf((String) request.getParameter(String.valueOf(amod.get(i).getId())))) == 0) {
                                                        mp.setModulo(amod.get(i));
                                                        new ModuloPerfilDAO().manterModPerfil(mp);
                                                    }
                                                }
                                                if ((String) request.getParameter("status") != null) {
                                                    if (Integer.valueOf((String) request.getParameter("status")) == Perfil.ATIVO) {
                                                        pfl.setStatus(Perfil.ATIVO);
                                                    } else {
                                                        pfl.setStatus(Perfil.INATIVO);
                                                    }
                                                    new PerfilDAO().atualizarPerfil(pfl);
                                                }
                                                if ((String) request.getParameter("descricao") != null) {
                                                    pfl.setDescricao((String) request.getParameter("descricao"));
                                                    new PerfilDAO().atualizarPerfil(pfl);
                                                }
                                            }
                                            out.println("<strong><center>Atualização realizada com sucesso.</center></strong><br>");
                                        } catch (Exception e) {
                                            System.out.println(e.getMessage());
                                            out.println("<strong><center>Falha na atualização: " + e.getMessage() + ".</center></strong><br>");
                                        }
                                    }
                        %>
                        <table border="1" cellspacing="0" width="500" bgcolor="#FFFFFF">
                            <tr>
                                <td width="100" colspan="2" align="center"><h3>Perfil: <% if (pfl != null) {
                                                out.println(pfl.getDescricao());
                                                out.println("<input type=\"hidden\" name=\"perfil\" value=\"" + pfl.getId() + "\">");
                                            }%></h3></td>
                            </tr>
                            <%
                                        ArrayList<Modulo> amod = new ModuloDAO().recuperarModulos(null);
                                        if ((amod != null) && (!amod.isEmpty())) {
                                            ModuloPerfil mp = new ModuloPerfil();
                                            mp.setPerfil(pfl);
                                            for (int i = 0; i < amod.size(); i++) {
                                                mp.setModulo(amod.get(i));
                                                ArrayList<ModuloPerfil> aux = new ModuloPerfilDAO().recuperarModPerfil(mp);
                                                out.println("<tr>");
                                                out.println("<td>" + amod.get(i).getGrupo() + " - " + amod.get(i).getSubGrupo() + " - " + amod.get(i).getDescricao() + ":</td>");
                                                if ((aux != null) && (!aux.isEmpty())) {
                                                    out.println("<td><center><input type=\"radio\" name=\"" + amod.get(i).getId() + "\" value=\"0\" checked>Sim   "
                                                            + "<input type=\"radio\" name=\"" + amod.get(i).getId() + "\" value=\"1\">Não</center></td>");
                                                } else {
                                                    out.println("<td><center><input type=\"radio\" name=\"" + amod.get(i).getId() + "\" value=\"0\">Sim   "
                                                            + "<input type=\"radio\" name=\"" + amod.get(i).getId() + "\" value=\"1\" checked>Não</center></td>");
                                                }
                                                out.println("</tr>");
                                            }
                                        }
                            %>
                            <tr>
                                <td>Status:</td>
                                <td align="center">
                                    <select name="status" size="1">
                                        <%
                                                    if (pfl != null) {
                                                        if (pfl.getStatus() == Perfil.ATIVO) {
                                                            out.println("<option value=\"" + Perfil.ATIVO + "\" selected>Ativo</option>");
                                                            out.println("<option value=\"" + Perfil.INATIVO + "\">Inativo</option>");
                                                        } else {
                                                            out.println("<option value=\"" + Perfil.INATIVO + "\" selected>Inativo</option>");
                                                            out.println("<option value=\"" + Perfil.ATIVO + "\">Ativo</option>");
                                                        }
                                                    }
                                        %>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Descrição:
                                </td>
                                <td>
                                    <input name="descricao" type="text" value="<%
                                                if (pfl != null) {
                                                    out.print(pfl.getDescricao());
                                                }
                                           %>">
                                </td>
                            </tr>
                            <tr>
                                <td colspan="2" align="center">
                                    <input type="submit" value="Atualizar">
                                </td>
                            </tr>
                        </table>
                    </form>
                </td>
            </tr>
        </table>
    </body>
</html>